<template>
  <view>
    <my-search :bgc="'skyblue'" :radius="18" @searchHandle="searchHandle"></my-search>
    <view class="category-contain">
      <scroll-view class="side-nav" scroll-y="=true" :style="{height: wh + 'px'}">
        <block v-for="(item, index) in categoryList" :key="index">
          <view :class="['nav-item', index === active ? 'active' : '']" @click=activeChange(index)>
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <scroll-view scroll-y="=true" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <block v-for="(item, index) in secondCategoryList" :key="index">
          <!-- 标题 -->
          <view class="second-category_title">/ {{item.cat_name}} /</view>

          <!-- 商品 -->
          <view class="shop-category" @click="goodsList(item)">
            <view class="shop-category_item" v-for="(shopItem, index) in item.children" :key="index">
              <!-- 商品图片 -->
              <image :src="shopItem.cat_icon"></image>
              <!-- 商品名 -->
              <text>{{shopItem.cat_name}}</text>
            </view>
          </view>
        </block>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  // 导入封装的 mixin 模块
  import cartBadge from '@/mixins/tabbar-badge.js'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [cartBadge],
    data() {
      return {
        wh: 0, // 当前设备可视高度
        categoryList: [], // 分类列表数据
        active: 0, // 当前激活项
        secondCategoryList: [], // 二级分类数据列表
        scrollTop: 0, // 利用滚动标签的属性
      }
    },

    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight - 50,
        this.getCategoryList()
    },

    methods: {
      async getCategoryList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.categoryList = res.message
        // 挂载完成默认选中第一个
        this.secondCategoryList = res.message[0].children
      },

      // 当前激活项
      activeChange(index) {
        this.active = index
        // 切换侧边一级导航
        this.secondCategoryList = this.categoryList[index].children
        // 切换左侧导航栏滚动条位置重置
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },

      // 查看商品详情
      goodsList(item) {
        // console.log(item)
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },

      // 搜索处理的自定义事件
      searchHandle() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .category-contain {
    display: flex;

    .side-nav {
      width: 120px;
      background-color: sandybrown;

      .nav-item {
        line-height: 60px;
        text-align: center;
        font-size: 14px;
        background-color: #f7f7f7;

        &.active {
          position: relative;
          background-color: #FFFFFF;

          &::before {
            content: ' ';
            display: block;
            position: absolute;
            width: 3px;
            height: 30px;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            background-color: #C00000;

          }
        }
      }
    }

    .second-category_title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      paddding: 15px 0;
    }

    .shop-category {
      display: flex;
      flex-wrap: wrap;

      &_item {
        width: 33.33%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-bottom: 10px;

        image {
          width: 120rpx;
          height: 120rpx;
        }

        text {
          font-size: 24rpx
        }
      }
    }
  }
</style>
